/*
 * TowerRedOne.java
 *
 * Created on December 9, 2007, 2:13 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package hextd.tower;

import hextd.Constants;
import hextd.creep.HexTDCreep;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author hylke
 */
public class TowerRedOne extends TowerBase {

	public static final Map<String, Long> price = new HashMap<String, Long>();

	static {
		price.put(Constants.RESOURCE_CREDIT, 200l);
	}
	public static int damage = 35;
	public static double range = 5f;
	public static double spreadRadiusBase = 2.25f;
	public static String name = "Red One";
	//
	private HexTDCreep fireAt = null;
	private Constants.COLOR damageColor = Constants.COLOR.RED;
	private int coolDown = 0;
	private int coolDownMax = 4;
	private Point2D.Double targetGLoc1 = new Point2D.Double();
	private Point2D.Double targetGLoc2 = new Point2D.Double();
	private Point2D.Double tempTargetPLoc1 = new Point2D.Double();
	//
	private Color transColor;
	private Shape spread;
	private double spreadRadius;
	//
	private HexTDCreep[] currentTargets;

	/** Creates a new instance of TowerRedOne */
	public TowerRedOne() {
		super(FactoryTower.type.redOne, price, damage, range);
		lineColor = Color.RED;
		hexInfluence = 1;
		transColor = new Color(lineColor.getRed(), lineColor.getGreen(), lineColor.getBlue(), 75);
	}

	@Override
	public void runTick(int gameTime) {
		if (coolDown > 0) {
			coolDown--;
			//if (coolDown > 2) {
			//	float radius = paintScale * spreadRadius;
			//	spread = new Ellipse2D.Float(fireAt.getPaintX() - radius, fireAt.getPaintY() - radius, radius * 2, radius * 2);
			//}
		} else {
			if (!lock) {
				fireAt = null;
			}
			HexTDCreep enemy = findEnemy(fireAt, false);
			fireAt = enemy;

			if (enemy != null) {
				double dx, dy, r;
				long splashDamage;
				fireAt.getGameLocation(targetGLoc1);
				currentTargets = findEnemiesInRange(targetGLoc1.x, targetGLoc1.y, spreadRadius);
				HexTDCreep enemy2;
				for (int i = 0; i < currentTargets.length; i++) {
					enemy2 = currentTargets[i];
					enemy2.getGameLocation(targetGLoc2);
					dx = targetGLoc1.x - targetGLoc2.x;
					dy = targetGLoc1.y - targetGLoc2.y;
					r = Math.sqrt(dx * dx + dy * dy);
					splashDamage = Math.round(damageCurrent * (1 - r / spreadRadius));
					enemy2.doDamage(getOwner(), splashDamage, damageColor);
				}

				//System.out.println("TR! fire at tick: "+gameTime);
				coolDown = coolDownMax;
				//float radius = paintScale * spreadRadius;
				//spread = new Ellipse2D.Float(fireAt.getPaintX() - radius, fireAt.getPaintY() - radius, radius * 2, radius * 2);
			}
		}
	}

	@Override
	public String getStatusString() {
		String retString = "";
		retString += "<h1>Red Tower 1</h1>"
				+ "<p>"
				+ "<b>Level:</b> " + (1 + level) + "<br>\n"
				+ "<b>Damage:</b> " + damageText + "<br>\n"
				+ "<b>Range:</b> " + rangeText + "<br>"
				+ "</p>\n";
		return retString;
	}

	@Override
	public String getDescription() {
		String retString = "";
		retString += "<h1>Red Tower 1</h1>"
				+ "<p>"
				+ "Tower with one laser. "
				+ "The impact of laser does splash damage to all enemies surrounding the target."
				+ "<br>Strong against red enemies, very weak against green enemies."
				+ "</p>"
				+ "<p>"
				+ "<b>Price:</b> " + TowerRedOne.price + "<br>"
				+ "<b>Damage:</b> " + damageCurrent + " + splash<br>"
				+ "<b>Range:</b> " + TowerRedOne.form.format(rangeCurrent) + "<br>"
				+ "<b>Refire rate:</b> 4/s"
				+ "</p>";
		return retString;
	}

	@Override
	protected void calcDamageRange() {
		super.calcDamageRange();
		spreadRadius = TowerRedOne.spreadRadiusBase;
	}

	@Override
	protected void generateShapes() {
		int shapeCount = 3;
		shapes = new Shape[shapeCount];
		shapes[0] = new Ellipse2D.Double(-0.5 * paintScale, -0.5 * paintScale, 1.0 * paintScale, 1.0 * paintScale);
		shapes[1] = new Ellipse2D.Double(-0.5 * paintScale, -0.5 * paintScale, 1.0 * paintScale, 1.0 * paintScale);
		shapes[2] = new Ellipse2D.Double(-1.0 * strokeScale, -1.0 * strokeScale, 2.0 * strokeScale, 2.0 * strokeScale);

		shapeAction = new int[shapeCount];
		shapeAction[0] = 2;
		shapeAction[1] = 1;
		shapeAction[2] = 1;

		shapeColor = new Color[shapeCount];
		shapeColor[0] = transColor;
		shapeColor[1] = lineColor;
		shapeColor[2] = lineColor;

		strokes = new Stroke[shapeCount];
		strokes[0] = new BasicStroke(2.0f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
		strokes[1] = strokes[0];
		strokes[2] = strokes[0];
	}

	@Override
	public void paintEffects(Graphics2D g2, int gameTime) {
		Stroke defaultStroke = g2.getStroke();

		if (fireAt != null && coolDown == coolDownMax) {
			g2.setColor(lineColor);
			fireAt.getPaintLocation(tempTargetPLoc1);
			double ex = tempTargetPLoc1.x + ((Math.random() - 0.5) * paintScale / 10f);
			double ey = tempTargetPLoc1.y + ((Math.random() - 0.5) * paintScale / 10f);
			g2.draw(new Line2D.Double(paintLocation.x, paintLocation.y, ex, ey));
			for (int i = 0; i < currentTargets.length; i++) {
				currentTargets[i].getPaintLocation(tempTargetPLoc1);
				g2.draw(new Line2D.Double(ex, ey, tempTargetPLoc1.x, tempTargetPLoc1.y));
			}
		}
		g2.setStroke(defaultStroke);
	}
}
